package com.yc.cloud.openai.service.impl;

import cn.hutool.core.io.FileUtil;
import com.yc.cloud.openai.dto.IATResultDTO;
import com.yc.cloud.openai.dto.NlpChunkResultDto;
import com.yc.cloud.openai.dto.OpenAiResultDto;
import com.yc.cloud.openai.dto.TTSResultDTO;
import com.yc.cloud.openai.enums.SupplierEnums;
import com.yc.cloud.openai.request.IatParam;
import com.yc.cloud.openai.request.NLPParam;
import com.yc.cloud.openai.request.OpenAiParam;
import com.yc.cloud.openai.request.TTSParam;
import com.yc.cloud.openai.service.IATService;
import com.yc.cloud.openai.service.NLPService;
import com.yc.cloud.openai.service.OpenAiService;
import com.yc.cloud.openai.service.tts.TTSService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import reactor.core.publisher.Flux;

import java.util.Map;

@Slf4j
@Service
public class LinkAIOpenAiServiceImpl implements OpenAiService {


    private final TTSService ttsService;
    private final IATService iatService;

    private final NLPService nlpService;


    public LinkAIOpenAiServiceImpl(@Qualifier("XFIatServiceImpl") IATService iatService, @Qualifier("linkAINLPServiceImpl") NLPService nlpService, @Qualifier("XFTTSServiceImpl") TTSService ttsService) {
        this.iatService = iatService;
        this.nlpService = nlpService;
        this.ttsService = ttsService;
    }

    @Override
    public String getIdentifier() {
        return SupplierEnums.LINK_AI.getCode();
    }

    @Override
    public OpenAiResultDto analysis(OpenAiParam openAiParam) {
        val openAiResultDto = new OpenAiResultDto();
        val iatResult = getIatResult(openAiParam);
        if (iatResult == null) {
            return null;
        }
        val iatContent = iatResult.getResult();
        openAiResultDto.setIatContent(iatContent);
        openAiResultDto.setIatCostTime(iatResult.getCostTime());
        //语言理解接口调用
        val nlpParam = new NLPParam();
        nlpParam.setContent(iatContent);
        nlpParam.setAppid(openAiParam.getAppid());
        nlpParam.setStream(openAiParam.getStream());
        nlpParam.setAppKey(openAiParam.getAppKey());
        val nlpResult = nlpService.getNLPResult(nlpParam);
        if (nlpResult == null) {
            return openAiResultDto;
        }
        val nlpContent = nlpResult.getResult();
        openAiResultDto.setNlpCostTime(nlpResult.getCostTime());
        openAiResultDto.setNlpContent(nlpContent);
        return openAiResultDto;
    }

    @Override
    public TTSResultDTO getTTSResult(OpenAiParam openAiParam) {
        val ttsParam = new TTSParam();
        ttsParam.setContent(openAiParam.getContent());
        ttsParam.setGender(openAiParam.getGender());
        ttsParam.setFormat(openAiParam.getFormat());
        val ttsResult = ttsService.getTTSResult(ttsParam);
        if (ttsResult == null) {
            return null;
        }
        return ttsResult;
    }

    @Override
    public Flux<String> analysisByStream(OpenAiParam openAiParam) {
        val iatResult = getIatResult(openAiParam);
        if (iatResult == null) {
            return null;
        }
        val nlpParam = new NLPParam();
        nlpParam.setContent(iatResult.getResult());
        nlpParam.setAppid(openAiParam.getAppid());
        nlpParam.setStream(openAiParam.getStream());
        nlpParam.setAppKey(openAiParam.getAppKey());
        return nlpService.getNLPResultByStream(nlpParam);
    }

    @Override
    public Flux<ServerSentEvent<String>> analysisByStream2(OpenAiParam openAiParam) {
        val iatResult = getIatResult(openAiParam);
        if (iatResult == null) {
            return null;
        }
        val nlpParam = new NLPParam();
        nlpParam.setContent(openAiParam.getContent());
        nlpParam.setAppid(openAiParam.getAppid());
        nlpParam.setStream(openAiParam.getStream());
        nlpParam.setAppKey(openAiParam.getAppKey());
        return nlpService.getNLPResultByStream2(nlpParam);
    }

    @Override
    public SseEmitter analysisByStream3(OpenAiParam openAiParam) {
        val iatResult = getIatResult(openAiParam);
        if (iatResult == null) {
            return null;
        }
        val nlpParam = new NLPParam();
        nlpParam.setContent(iatResult.getResult());
        nlpParam.setAppid(openAiParam.getAppid());
        nlpParam.setStream(openAiParam.getStream());
        nlpParam.setAppKey(openAiParam.getAppKey());
        return nlpService.getNLPResultByStream3(nlpParam);
    }

    private IATResultDTO getIatResult(OpenAiParam openAiParam) {
        val path = openAiParam.getPath();
//        //将audio文件转成base64
//        val base64Data = MyFileUtils.convertToBase64(FileUtil.file(path));
        //语音文件转音频文本内容
        val iatParam = new IatParam();
        iatParam.setAppid(openAiParam.getIatAppid());
        iatParam.setAppKey(openAiParam.getIatAppKey());
        iatParam.setContent(path);
        iatParam.setFormat(FileUtil.getSuffix(path));
        return iatService.getIatResult(iatParam);
    }
}
